//
//  ZCPublishViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/9/1.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCPublishViewController.h"
#import "ZCVerticalButton.h"
#import "ZCPublishTextViewController.h"
#import "ZCNavigationController.h"

typedef enum : NSUInteger {
    PublishVideoBtn = 0,
    PublishPictureBtn = 1,
    PublishTextBtn = 2,
    PublishVoiceBtn = 3,
    PublishShenTieBtn = 4,
    PublishLinkBtn = 5
} PublishButtonType;


@interface ZCPublishViewController ()

@property (strong, nonatomic) NSMutableArray<UIButton*> *buttonArray;
@property (weak, nonatomic) UIButton *cancelBtn;

@end

@implementation ZCPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(NSArray<UIButton *> *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

-(void)setupUI{
    
    self.view.backgroundColor = ZCGlobalRBG;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancel)];
    [self.view addGestureRecognizer:tapGesture];
    
    //取消按钮
    UIButton *cancelBtn = [UIButton buttonWithImage:@"shareButtonCancel" highlightedImage:@"shareButtonCancelClick"];
    self.cancelBtn = cancelBtn;
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-30);
    }];
    
    //标语
    UIButton *titleLab = [UIButton buttonWithBackgroundImage:@"app_slogan"];
    [self.view addSubview:titleLab];
    titleLab.centerX = ScreenWith/2;
    titleLab.centerY = ScreenHeight*0.2;
    

    NSArray *pic = @[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
    NSArray *title = @[@"发视频",@"发照片",@"发段子",@"发声音",@"发审帖",@"发连接"];
        
    CGFloat buttonW = 72;
    CGFloat buttonH = buttonW + 30;
    CGFloat marginX = (ScreenWith - 3*buttonW)/3;
    CGFloat marginY = 60;
    for (NSInteger i = 0; i<title.count; i++) {
        ZCVerticalButton *button = [[ZCVerticalButton alloc]init];
        button.tag = i;
        button.adjustsImageWhenHighlighted = NO;
        [button setImage:[UIImage imageNamed:pic[i]] forState:UIControlStateNormal];
        [button setTitle:title[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        NSInteger row = i/3;
        NSInteger loc = i%3;
        CGFloat buttonX = marginX/2 +loc*(buttonW+marginX);
        
        CGFloat buttonEndY = CGRectGetMaxY(titleLab.frame)+ marginY + row*(marginY/2+buttonH);
        
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, 0, buttonW, buttonH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)];
        anim.springBounciness = 10;
        anim.springSpeed = 10;
        anim.beginTime = CACurrentMediaTime()+i*0.1;
        [button pop_addAnimation:anim forKey:nil];
        
        [self.buttonArray addObject:button];
        
        
    }
   
}


-(void)cancel{
    
    [self cancelBtnActionWith:nil];
}
-(void)cancelBtnActionWith:(void(^)())completionBlock{
    
    self.cancelBtn.hidden = YES;
    NSInteger i = self.buttonArray.count;
    for (UIButton*button in [self.buttonArray objectEnumerator]) {
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
        anim.toValue = [NSValue valueWithCGPoint:CGPointMake(button.centerX, ScreenHeight+button.width)];
        anim.springBounciness = 10;
        anim.springSpeed = 10;
        anim.beginTime = CACurrentMediaTime()+(i--)*0.1;
        [button pop_addAnimation:anim forKey:nil];
        
        if (i == 0) {
    
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                
                [self dismissViewControllerAnimated:NO completion:nil];
                
                !completionBlock ? : completionBlock();
            }];
        }
    }
    
    

    
}
-(void)buttonAction:(UIButton*)sender{
    
    [self cancelBtnActionWith:^{
        switch (sender.tag) {
            case PublishVideoBtn:
                ZCLog(@"发视频");
                break;
            case PublishPictureBtn:
                ZCLog(@"发照片");
                break;
            case PublishTextBtn:{
                ZCLog(@"发段子");
                ZCPublishTextViewController *textVC = [[ZCPublishTextViewController alloc]init];
                ZCNavigationController *nav = [[ZCNavigationController alloc]initWithRootViewController:textVC];
                [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];}
                break;
            case PublishVoiceBtn:
                ZCLog(@"发声音");
                break;
            case PublishShenTieBtn:
                ZCLog(@"发审帖");
                break;
            case PublishLinkBtn:
                ZCLog(@"发链接");
                break;
            default:
                break;
        }
    }];
}



@end
